package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusScholartyrant;
import com.meida.module.bus.provider.service.BusScholartyrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("ScholarTyrantSaveHandler")
public class ScholarTyrantSaveHandler implements SaveInterceptor<BusScholartyrant> {

    @Autowired
    private BusScholartyrantService busScholartyrantService;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap params) {
        Long userId = OpenHelper.getUserId();
        Long num = busScholartyrantService.countNumByUserId(userId);
        if (num > 0) {
            return ResultBody.failed("你已提交过学霸认证");
        }
        String userName = params.get("userName");
        String email = params.get("email");
        String subjectInfo = params.get("subjectInfo");
        if (FlymeUtils.isEmpty(userName)) {
            return ResultBody.failed("请填写你的真实姓名");
        }
        if (FlymeUtils.isEmpty(email)) {
            return ResultBody.failed("请填写你的邮箱");
        }
        if (FlymeUtils.isEmpty(subjectInfo)) {
            return ResultBody.failed("请选择你擅长的题目");
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusScholartyrant busScholartyrant) {
        Long userId = OpenHelper.getUserId();
        busScholartyrant.setScholartyrantId(userId);
    }

}
